package com.gt.xmall.log;

import java.util.Date;


import org.hibernate.event.spi.PostDeleteEvent;
import org.hibernate.event.spi.PostDeleteEventListener;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import com.gt.xmall.log.model.SysDataLog;
import com.gt.xmall.log.repository.SysDataLogRepository;
import com.gt.xmall.util.ApplicationUtil;

public class DataListener implements PostInsertEventListener,
		PostUpdateEventListener, PostDeleteEventListener {
	
	SysDataLogRepository sysDataLogRepository;

	MongoTemplate mongoTemplate ;
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	

	public void onPostDelete(PostDeleteEvent event) {
		
		try {
			// TODO Auto-generated method stub
			sysDataLogRepository =(SysDataLogRepository) ApplicationUtil.getSpringBeanByName( "applicationContext-mongo.xml","sysDataLogRepository");
			SysDataLog sysDataLog =new SysDataLog();

			sysDataLog.setOperateType("delete");
			 sysDataLog.setEntityName(event.getEntity().getClass().getSimpleName());
			 
			 sysDataLog.setEntityId(event.getId().toString());
			 sysDataLog.setOperateTime(new Date());
			 
			 for (int i = 0; i < event.getDeletedState().length; i++) {
				 sysDataLog.setId(null);
				 Object oldValue = event.getDeletedState()[i];
				 if(oldValue !=null){
					 sysDataLog.setPreviousValue(oldValue.toString());
				 }else{
					 sysDataLog.setPreviousValue(null);
				 }
				 String propertyName = event.getPersister().getPropertyNames()[i];
			     sysDataLog.setColumnName(propertyName);
			     sysDataLogRepository.save(sysDataLog);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.info("onPostDelete>>>>>>>>>>>>>>>");
		}
	}

	public void onPostUpdate(PostUpdateEvent event) {
		// TODO Auto-generated method stub
		try {
			sysDataLogRepository =(SysDataLogRepository) ApplicationUtil.getSpringBeanByName( "applicationContext-mongo.xml","sysDataLogRepository");
			SysDataLog sysDataLog =new SysDataLog();
			logger.info("onPostUpdate");
			
			
			sysDataLog.setOperateType("update");
			 sysDataLog.setEntityName(event.getEntity().getClass().getSimpleName());
			 
			 sysDataLog.setEntityId(event.getId().toString());
			 sysDataLog.setOperateTime(new Date());
			 
			 for (int i = 0; i < event.getState().length; i++) {
				 sysDataLog.setId(null);
				// 鏇存柊鍓嶇殑鍊�            
				 Object oldValue = event.getOldState()[i];
			     if(oldValue !=null){
			    	 sysDataLog.setPreviousValue(oldValue.toString());
			     }else{
			    	 sysDataLog.setPreviousValue(null);
			     }
			     
			     // 鏇存柊鍚庣殑鏂板�
			     Object newValue = event.getState()[i];
			     if(newValue !=null){
			    	 sysDataLog.setNewValue(newValue.toString());
			     }else{
			    	 sysDataLog.setNewValue(null);
			     }
			     
			     
			     String propertyName = event.getPersister().getPropertyNames()[i];
			     sysDataLog.setColumnName(propertyName);
			     logger.info("onPostUpdate>>>>>>>>>>>"+sysDataLog);
			     
			     sysDataLogRepository.save(sysDataLog);
			     
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.info("onPostUpdate>>>>>>>>>>>>>>>");
		}
		
		
	}

	public void onPostInsert(PostInsertEvent event) {
		try {
			// TODO Auto-generated method stub
			
			sysDataLogRepository =(SysDataLogRepository) ApplicationUtil.getSpringBeanByName( "applicationContext-mongo.xml","sysDataLogRepository");
			SysDataLog sysDataLog =new SysDataLog();
			sysDataLog.setOperateType("delete");
			 sysDataLog.setEntityName(event.getEntity().getClass().getSimpleName());
			 
			 sysDataLog.setEntityId(event.getId().toString());
			 sysDataLog.setOperateTime(new Date());
			 
			 for (int i = 0; i < event.getState().length; i++) {
				 sysDataLog.setId(null);
				 Object newValue = event.getState()[i];
				 if(newValue !=null){
					 sysDataLog.setNewValue(newValue.toString());
				 }else{
					 sysDataLog.setNewValue(null);
				 }
				 String propertyName = event.getPersister().getPropertyNames()[i];
			    sysDataLog.setColumnName(propertyName);
			    sysDataLogRepository.save(sysDataLog);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.info("onPostInsert>>>>>>");
		}
	}

}
